package com.github.slprj.slake.common;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Des:
 *
 * @author captain
 * @date 2022/8/26 16:37
 */
public class SlThreadFactory implements ThreadFactory {
    private final String prefix;
    //不存在非daemon线程时, 结束进程
    private final boolean daemon;
    private final AtomicInteger count = new AtomicInteger();

    public SlThreadFactory(String prefix, boolean daemon) {
        this.prefix = prefix;
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(this.daemon);
        t.setName(prefix + count.getAndIncrement());
        return t;
    }
}
